package com.ansion.baseui.base.toolbar;

import android.content.res.ColorStateList;
import android.net.Uri;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.ansion.baseui.base.view.SimpleDraweeViewFix;
import com.ansion.utils.ResourceUtils;
import com.ansion.utils.UiUtils;
import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.imagepipeline.request.ImageRequest;


/**
 * created by zhaoyuntao
 * on 10/01/2023
 * description:
 */
public class TActionBarMenuUtil {
    public static void setMenuParams(@NonNull TextView textView, @NonNull TMenuItem menuItem) {
        if (menuItem.hasText()) {
            textView.setText(menuItem.getText());
        } else if (menuItem.hasTextRes()) {
            textView.setText(menuItem.getTextRes());
        }
        if (menuItem.hasTextColor()) {
            textView.setTextColor(menuItem.getTextColor());
        } else if (menuItem.hasTextColorRes()) {
            textView.setTextColor(ResourceUtils.getColor(menuItem.getTextColorRes()));
        }
        textView.setEnabled(menuItem.isEnable());
        menuItem.setUiCallback((TMenuItem t) -> setMenuParams(textView, t));
    }

    public static void setMenuParams(@NonNull ImageView imageView, @NonNull TMenuItem menuItem) {
        if (menuItem.hasDrawableRes()) {
            imageView.setImageResource(menuItem.getDrawableRes());
        } else if (menuItem.hasDrawable()) {
            imageView.setImageDrawable(menuItem.getDrawable());
        }

        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        if (menuItem.hasDrawableColor()) {
            imageView.setImageTintList(ColorStateList.valueOf(menuItem.getDrawableColor()));
        } else if (menuItem.hasDrawableColorRes()) {
            imageView.setImageTintList(ColorStateList.valueOf(ResourceUtils.getColor(menuItem.getDrawableColorRes())));
        } else {
            imageView.setImageTintList(null);
        }
        imageView.setEnabled(menuItem.isEnable());
        menuItem.setUiCallback((TMenuItem t) -> setMenuParams(imageView, t));
    }

    public static void setMenuParams(@NonNull SimpleDraweeViewFix imageView, @NonNull TMenuItem menuItem) {
        imageView.getHierarchy().setActualImageScaleType(ScalingUtils.ScaleType.FIT_CENTER);
        int size = UiUtils.dipToPx(42);
        if (menuItem.hasDrawableURL()) {
            imageView.setImageURIWithSize(Uri.parse(menuItem.getDrawableURL()), size, size, ImageRequest.CacheChoice.SMALL);
        }
        if (menuItem.hasDrawableColor()) {
            imageView.setColorFilter(menuItem.getDrawableColor());
        } else if (menuItem.hasDrawableColorRes()) {
            imageView.setColorFilter(ResourceUtils.getColor(menuItem.getDrawableColorRes()));
        } else {
            imageView.setColorFilter(null);
        }
        imageView.setEnabled(menuItem.isEnable());
        menuItem.setUiCallback((TMenuItem t) -> setMenuParams(imageView, t));
    }
}
